

student_info = []   #列表用来存放所有学生信息
def start_page():
    print("      学生管理系统 V1.0")
    print(" 1:添加学生")
    print(" 2:删除学生")
    print(" 3:修改学生")
    print(" 4:查询学生")
    print(" 5:显示所有学生")
    print(" 6:保存数据")
    print(" 7:退出系统")
    print("---------------------------")
    return

def input_info():                              #定义函数input_info 实现信息输入
    student = {}                               # 字典studen用来存放每个学生的信息
    student_id = input("请输入学生学号")
    student = {}
    for student_dict in student_info:          #遍历学生信息列表，得到每一个学生的字典
        if student_id in student_dict.values():   #判断学号是否重复
            print('该学号已被占用，请重新输入')
            return                             # 如果重复，做出提示后直接结束
    student['ID'] = student_id
    student['Name'] = input("请输入学生姓名")
    student['TEL'] = input("请输入学生联系方式")
    student_info.append(student)




def delete_info():                                 #信息删除
    student_id = input('请输入需要删除的学生学号')
    for student_dict in student_info:
        if student_id in student_dict['ID']:
            confirm = input('您确定要删除该条信息吗？Y/N')
            if confirm !='Y' and confirm !='N':
                    print('输入错误，请重新操作')
            elif confirm == 'Y':
                    student_info.remove(student_dict)
                    print('信息删除成功')
            else:
                    print('已取消操作')
            return
    print('未找到相关记录。请确认后重新操作')

def change_info():                                       # 修改信息  默认学号不可变
    new_student = {}                                     #定义一个新的字典存放修改后的信息
    student_id = input('请输入修改学生的学号')
    for student_dict in student_info:                   # 遍历列表
        print(student_dict)
        if student_id in student_dict.values():         # 若需要修改的学生学号存中，输入新的信息
            new_student['ID'] = student_id              # 如果需要改学号，改一下这行代码即可
            new_student['Name'] = input("请输入新的学生姓名")
            new_student['TEL'] = input("请输入新的学生联系方式")
            confirm = input('您确定要进行修改吗？Y/N')      #操作确认
            if confirm !='Y' and confirm !='N':
                    print('输入错误，请重新操作')
            elif confirm == 'Y':                        #确定修改后，删除原信息，增加信息
                    student_info.remove(student_dict)
                    student_info.append(new_student)
                    # student_dict['ID'] = new_student['ID']   # 或者直接更改原来的信息
                    # student_dict['Name'] = new_student['Name']
                    # student_dict['TEL'] = new_student['TEL']
                    print('信息修改成功')
            else:
                    print('已取消操作')
            return

    print('未找到相关记录。请确认后重新操作')

def select_info():
    method = input('请选择查询方式：按学号查询（1），按姓名查询（2），按电话号码查询（3）')
    if method == 1:
        student_id = input("请输入要查询的学号")
        for student_dict in student_info:
            if student_id in student_dict['ID']:
                print(student_dict)
            else:
                print('未查询到相关记录')
    if method == 2:
        student_name = input("请输入要查询的学生姓名")
        for student_dict in student_info:
            if student_name in student_dict['Name']:
                print(student_dict)
            else:
                print('未查询到相关记录')
    if method == 3:
        student_tel = input("请输入要查询的学号")
        for student_dict in student_info:
            if student_tel in student_dict['ID']:
                print(student_dict)
            else:
                print('未查询到相关记录')
    else:
        print('输入错误')


def show_info():
    print(student_info)

def save_info():
    file = open('student_info.txt',mode='w',encoding='utf-8')
    for student_dict in student_info:
        file.write(str(student_dict))
    file.close()

UserInput = 1
start_page()
while UserInput != 7:
    UserInput =int(input('选择功能'))
    if UserInput not in range(1, 8):
        print('输入错误,请重新输入')
    elif UserInput == 1:
        input_info()
        print(student_info)
    elif UserInput == 2:
        print(student_info)
        delete_info()
    elif UserInput == 3:
        change_info()
    elif UserInput == 4:
        select_info()
    elif UserInput == 5:
        show_info()
    elif UserInput == 6:
        save_info()


